<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Unit_organisasi_model extends CI_Model {

	function __construct()
	{
		parent::__construct();
		$this->CI = get_instance();
	}
	
	function insert_unit($data){
		$this->db->insert('unit_organisasi',$data);
	}
	
	function get_unit_grid(){
		
		//Select table column
		$table_column = 'o.ID_UNIT_ORGANISASI AS ID_UNIT_ORGANISASI,';
		$table_column .= 'o.NAMA_UNIT AS NAMA_UNIT,';
		$table_column .= 'o.ESELON AS ESELON,';
		$table_column .= 'u.JABATAN AS JABATAN';
		
		$this->db->select($table_column);
		$this->db->from('unit_organisasi o');
		$this->db->join('users u','u.ID_USER = o.DIREKTUR','left outer');
		$this->db->where("o.STATUS > 0 ");
		$this->CI->flexigrid->build_query();
		
		$return['records'] = $this->db->get();
		
		$this->db->select($table_column);
		$this->db->from('unit_organisasi o');
		$this->db->join('users u','u.ID_USER = o.DIREKTUR','left outer');
		$this->db->where("o.STATUS > 0 ");
		$this->CI->flexigrid->build_query(FALSE);
		
		$return['record_count'] = $this->db->count_all_results();

		return $return;
	}
	
	function delete_unit($id_unit){
		$data = array(
			'STATUS' 			=> 0
		);
		$this->db->where('ID_UNIT_ORGANISASI',$id_unit);
		$this->db->update('unit_organisasi',$data);
	}
	
	function get_unitID($id_unit){
		
		//Select table column
		$table_column = 'o.ID_UNIT_ORGANISASI AS ID_UNIT_ORGANISASI,';
		$table_column .= 'o.NAMA_UNIT AS NAMA_UNIT,';
		$table_column .= 'o.ESELON AS ESELON,';
		$table_column .= 'u.JABATAN AS JABATAN,';
		$table_column .= 'o.DIREKTUR AS DIREKTUR';
		
		$this->db->select($table_column);
		$this->db->from('unit_organisasi o');
		$this->db->join('users u','u.ID_USER = o.DIREKTUR','left outer');
		$this->db->where('o.ID_UNIT_ORGANISASI',$id_unit);
		$query = $this->db->get();
		return $query;
	}
	
	function update_unit($data,$id_unit){
		$this->db->where('ID_UNIT_ORGANISASI',$id_unit);
		$this->db->update('unit_organisasi',$data);
	}
	
	function get_unit_organisasi($eselon)
	{
		$unitid = $this->session->userdata('unitid');
		
		$this->db->select('*');
		$this->db->from('unit_organisasi');
		$this->db->where('ESELON',$eselon);
		// catch if the $unitid is Admin or Not
		if($unitid >= 0) $this->db->where('ID_UNIT_ORGANISASI', $unitid);
		$this->db->where("STATUS > 0 ");
		$query = $this->db->get();
		return $query;
	}
	
	function getall_unit_org(){
		$this->db->select('*');
		$this->db->from('unit_organisasi');
		$this->db->where("STATUS > 0");
		$query = $this->db->get();
		return $query;
	}
	
}

?>
